package org.iteam.xlz.manager.action.message;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.web.shiro.ShiroUtil;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.web.struts.action.RestAction;
import org.iteam.xlz.core.entity.messages.Messages;
import org.iteam.xlz.core.service.message.MessageService;

@SuppressWarnings("unchecked")
@Results({@Result(name="INDEX",type="freemarker",location="/WEB-INF/content/message/message/index.html"),
	      @Result(name="EDIT",type="freemarker",location="/WEB-INF/content/message/message/edit.html")})
public class MessageAction extends AbstractModuleAction<Messages> implements RestAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4603285546897591101L;
	private final String PATH="/message/message";
	
	@Override
	public Messages getModel() {
		if(null==model){
			model=new Messages();
		}
		return model;
	}
	@Override
	@Action(PATH+"/index")
	public String index() {
		return INDEX;
	}

	@Override
	@Action(PATH+"/query")
	public String query() {
		map.put("total",MessageService.getInstance().counts());
		map.put("rows",MessageService.getInstance().query(page,rows));
		return MAP;
	}

	@Override
	@Action(PATH+"/create")
	public String create() {
		FileReader fr=null;
		String mobiles ="";
		try {
			if(null!=file){
				fr = new FileReader(file);
				BufferedReader reader=new BufferedReader(fr);
				String mobile=null;
				boolean flag=true;
				while((mobile=reader.readLine())!=null){
					mobile=mobile.trim();
					try{
						Long.parseLong(mobile);
						if(mobile.length()==11){
							mobiles+=mobile+",";
						}else{
							flag=false;
						}
					}catch(NumberFormatException ne){
						flag=false;
					}

					if(!flag){
						map.put("state",flag);
						map.put("info","文件格式错误");
						reader.close();
						return MAP;
					}
				}
				reader.close();	
			}
			model.setCreator(ShiroUtil.currentAccount());
			map.put("state",MessageService.getInstance().create(model,mobiles,overduetime));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(null!=fr){
					fr.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return MAP;
	}

	@Override
	@Action(PATH+"/update")
	public String update() {
		map.put("state",MessageService.getInstance().update(model));
		return MAP;
	}

	@Override
	public String delete() {
		map.put("state",MessageService.getInstance().delete(_id));
		return MAP;
	}

	@Override
	public String destroy() {
		return null;
	}

	@Override
	@Action(PATH+"/{_id}/edit")
	public String edit() {
		super.object=MessageService.getInstance().find(_id);
		return EDIT;
	}

	@Override
	@Action(PATH+"/new")
	public String editNew() {
		return EDIT;
	}
	@Override
	public String show() {
		return null;
	}

	private File file;
	private String overduetime;

	public void setFile(File file) {
		this.file = file;
	}
	public void setOverduetime(String overduetime) {
		this.overduetime = overduetime;
	}
}
